package com.suncaper.wcss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suncaper.wcss.common.BaseConstant;
import com.suncaper.wcss.common.PageDTO;
import com.suncaper.wcss.common.utls.SessionUtils;
import com.suncaper.wcss.model.domain.*;
import com.suncaper.wcss.mapper.ProductMapper;
import com.suncaper.wcss.model.dto.ProductDTO;
import com.suncaper.wcss.model.vo.ProductVO;
import com.suncaper.wcss.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.suncaper.wcss.common.BaseConstant;

import static com.suncaper.wcss.common.BaseConstant.*;

/**
 * <p>
 * 衣服款式表 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2021-11-12
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductImageService productImageService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private BatchSettingService batchSettingService;
    @Autowired
    private StudentDataService studentDataService;

    @Override
    @Transactional
    public void insert(ProductDTO product) {
        baseMapper.insert(product);
        //需要根据url得到image对象，这个对象需要两个值，一个路径，一个衣服id
        List<ProductImage> productImages = product.getImgs().stream().map(x -> new ProductImage().setProductId(product.getId()).setPath(x)).collect(Collectors.toList());
        productImageService.saveBatch(productImages);
        //现在还有规格
        List<Sku> skus = product.getSkus().stream().map(x -> new Sku().setProductId(product.getId()).setName(x)).collect(Collectors.toList());
        skuService.saveBatch(skus);
    }

    @Override
    public Page<ProductVO> pageList(PageDTO page, Product product) {
        if(Objects.isNull(product.getBatchId())){
            //后台获取当前批次
            BatchSetting batchSetting = batchSettingService.curBatch();
            product.setBatchId(batchSetting.getId());
        }
        //到底是学生，还是管理员
        User user = SessionUtils.getCurrentUserInfo();
        //如果是学生，给参数多加一个性别条件
        if(ROLE_STUDENT.equals(user.getRoleId())){
            //获取当前登录用户的性别
            QueryWrapper<StudentData> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(StudentData::getSn,user.getSn());
            StudentData one = studentDataService.getOne(wrapper);
            product.setGender(SEX_VALUE_M.equals(one.getGender())?SEX_KEY_M:SEX_KEY_F);
        }
        List<ProductVO> productVOS = baseMapper.pageList(product);
        Page<ProductVO> voPage = new Page<>(page.getPageNo(), page.getPageSize());
        voPage.setRecords(productVOS);
        return voPage;
    }

    @Override
    public ProductVO selectById(Long id) {
        return baseMapper.getById(id);
    }

    @Override
    @Transactional
    public void edit(ProductDTO productDTO) {
        //修改寒衣表
        baseMapper.updateById(productDTO);
        //删除规格表
        QueryWrapper<Sku> skuQueryWrapper = new QueryWrapper<>();
        skuQueryWrapper.lambda().eq(Sku::getProductId,productDTO.getId());
        skuService.remove(skuQueryWrapper);
        //更新sku
        List<Sku> skus = productDTO.getSkus().stream().map(x -> new Sku().setProductId(productDTO.getId()).setName(x)).collect(Collectors.toList());
        skuService.saveBatch(skus);
        //删除图片
        QueryWrapper<ProductImage> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ProductImage::getProductId,productDTO.getId());
        productImageService.remove(wrapper);
        //添加图片
        List<ProductImage> productImages = productDTO.getImgs().stream().map(x -> new ProductImage().setProductId(productDTO.getId()).setPath(x)).collect(Collectors.toList());
        productImageService.saveBatch(productImages);
    }
}
